package com.yami.shop.platform.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Blog;
import com.yami.shop.bean.model.Tag;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.BlogService;
import com.yami.shop.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/admin/tag")
public class TagController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/page")
    @Operation(summary =  "根据参数分页获取广场标签列表", description = "根据参数分页获取广场标签列表")
    public ServerResponseEntity<IPage<Tag>> page(PageParam<Tag> page, Tag blog) {
        LambdaQueryWrapper<Tag> tagQueryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(blog.getStatus()) && blog.getStatus() != 0){
            tagQueryWrapper.eq(Tag::getStatus,blog.getStatus());
        }else{
            tagQueryWrapper.ne(Tag::getStatus,0);
        }
        if(StrUtil.isNotEmpty(blog.getContent())){
            tagQueryWrapper.like(Tag::getContent,blog.getContent());
        }
        tagQueryWrapper.orderByAsc(Tag::getSort).orderByDesc(Tag::getCreateTime);
        PageParam<Tag> tagList = tagService.page(page, tagQueryWrapper);
        return ServerResponseEntity.success(tagList);
    }

    @PostMapping
    @Operation(summary =  "新增广场标签", description = "新增广场标签")
    public ServerResponseEntity<Void> saveTag(@RequestBody Tag tag){
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content", tag.getContent());
        queryWrapper.eq("status", 1);
        Tag tempTag = tagService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该标签已存在");
        }
        tag.setStatus(1);
        tagService.save(tag);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary =  "修改广场标签", description = "修改广场标签")
    public ServerResponseEntity<Void> updateTag(@RequestBody Tag tag){
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content", tag.getContent());
        queryWrapper.ne("uid",tag.getUid());
        queryWrapper.eq("status", 1);
        Tag tempTag = tagService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该标签已存在");
        }
        tagService.updateById(tag);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{uids}")
    @Operation(summary =  "删除", description = "新增广场分类")
    public ServerResponseEntity<Void> delBatchTag(@PathVariable("uids") Long[] uids){
        List<Tag> blogs = tagService.listByIds(Arrays.asList(uids));
        List<Long> collect1 = blogs.stream().map(a -> a.getUid()).collect(Collectors.toList());
        LambdaQueryWrapper<Blog> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogLambdaQueryWrapper.in(Blog::getBlogSortUid,collect1);
        blogLambdaQueryWrapper.ne(Blog::getStatus,0);
        long count = blogService.count(blogLambdaQueryWrapper);
        if(count>0){
            return ServerResponseEntity.showFailMsg("选中的正在广场中使用");
        }
        blogs.forEach(blog->blog.setStatus(0));
        tagService.updateBatchById(blogs);
        return ServerResponseEntity.success();
    }
}
